<%--
  Created by 邓浩阳.
  Date: 2018/3/22
  Time: 20:28
  内容发布页面
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@include file="common.jspf"%>
<html>
<head>
    <title>Title</title>
    <script src="${hkxyPath}/resources/ckeditor/ckeditor.js"></script>
    <script src="${hkxyPath}/resources/ckeditor/config.js"></script>
    <script>
        $(function(){
            var json='${json}';
            console.log(json);
            if(json!=null||json!=""){
                var obj=jQuery.parseJSON('${json}');
                articleID=obj.id;
                var title=obj.title;
                $("#name").val(title);
                var content=htmldecode(obj.content);
                $("#content").val(content); $("#inputfile").val(docurl);
                }
            $(".close").click(function(){
                $("#errorAlert").alert('close');
            })
        })
        function ajaxSubmit(data){
            for(instance in CKEDITOR.instances){
                CKEDITOR.instances[instance].updateElement();
            }
            var formData = new FormData($("#form")[0]);
          //  formData.append('file',$("#form")[0]);    //将文件转成二进制形式
            $.ajax({
                type: "POST",
                dataType: "json",//预期服务器返回的数据类型
                url: "${hkxyPath}/do-${type}-publish?state="+data ,
                data: formData,
                contentType: false,    //这个一定要写
                processData: false, //这个也一定要写，不然会报错
                success: function (result) {
                    if(result.code==1){
                    console.log("success \n"+result);//打印服务端返回的数据(调试用)
                    $("#successAlert").removeClass("hidden");
                    alert("提交成功！");
                    setTimeout(function(){
                        $("#main").load("${hkxyPath}/${type}-manage",null,null);
                        history.pushState(null,null,"${hkxyPath}/${type}-manage")
                    },2000);
                    }else {
                        console.log("error \n"+result);
                        alert("提交失败！");
                        $("#errorAlert").removeClass("hidden");
                    }
                }
            })
        }
    </script>
</head>
<body>
<div id="writeMain">
    <div id="successAlert" class="alert alert-success hidden">
        <strong>成功！</strong>提交成功！\n正在跳转至文章管理页面...
    </div>
    <div id="errorAlert" class="alert alert-warning hidden">
        <a href="#" class="close" data-dismiss="alert">&times;</a>
        <strong>失败！</strong>提交失败，请检查网络连接。
    </div>

    <div style="height: 60px">
        <h2>内容发布</h2>
    </div>

    <form id="form" role="form" class="padding-5"
        <%--action="${hkxyPath}/do-${type}-publish" method="post"--%>
          enctype="multipart/form-data">
        <div class="form-group">
            <label for="name" class="input-control input-control-title padding-b-5">题目</label>
            <input name="title" type="text" class="form-control" id="name" placeholder="请输入题目，30个字符以内">
         </div>
        <div class="form-group">
            <label for="name" class="padding-b-5">内容</label>
            <textarea class="form-control" name="content" id="content" style="height: 500px"></textarea>
            <script type="text/javascript">
                editor=CKEDITOR.replace('content',{
                    height:500,
                    filebrowserImageUploadUrl:'${hkxyPath}/upload/image',
                    language : 'zh-cn'
                });
            </script>
        </div>
        <div class="form-group inline-form">
            <label for="inputfile" class="padding-b-5">文件上传</label>
            <input type="file" id="inputfile" name="file">
            <span class="help-block">如有需要可进行文件上传，并在前台提供下载功能。</span>
        </div>
        <div class="inline-form text-right">
            <button type="button" name="state" class="btn btn-info btn-lg" value="1" onclick="ajaxSubmit(1)">发布</button>
            <button type="button" name="state" value="0" class="btn btn-default btn-lg" onclick="ajaxSubmit(0)">存草稿</button>
        </div>
    </form>
</div>
</body>
</html>
